import AITest from './aiTest'

/**
 * 运行AI测试
 */
export function runAITests() {
  console.log('🚀 启动AI测试套件...')
  
  const aiTest = new AITest()
  
  try {
    // 运行所有测试
    aiTest.runAllTests()
    
    // 运行性能基准测试
    console.log('\n📊 运行性能基准测试...')
    const benchmark = aiTest.benchmarkPerformance()
    
    // 输出详细报告
    console.log('\n📋 详细测试报告:')
    console.log('=' * 50)
    
    for (const result of aiTest.testResults) {
      const emoji = result.status === 'PASS' ? '✅' : '❌'
      console.log(`${emoji} ${result.testName}`)
      console.log(`   状态: ${result.status}`)
      console.log(`   消息: ${result.message}`)
      console.log(`   时间: ${result.timestamp}`)
      console.log('')
    }
    
    console.log('📊 性能基准测试结果:')
    console.log(`   平均时间: ${benchmark.average.toFixed(2)}ms`)
    console.log(`   最短时间: ${benchmark.min}ms`)
    console.log(`   最长时间: ${benchmark.max}ms`)
    console.log(`   标准差: ${calculateStandardDeviation(benchmark.times).toFixed(2)}ms`)
    
    return {
      success: true,
      results: aiTest.testResults,
      benchmark
    }
    
  } catch (error) {
    console.error('❌ 测试运行失败:', error)
    return {
      success: false,
      error: error.message
    }
  }
}

/**
 * 计算标准差
 */
function calculateStandardDeviation(values) {
  const mean = values.reduce((a, b) => a + b, 0) / values.length
  const squaredDiffs = values.map(value => Math.pow(value - mean, 2))
  const variance = squaredDiffs.reduce((a, b) => a + b, 0) / values.length
  return Math.sqrt(variance)
}

/**
 * 导出测试函数供外部调用
 */
if (typeof window !== 'undefined') {
  window.runAITests = runAITests
}

if (typeof GameGlobal !== 'undefined') {
  GameGlobal.runAITests = runAITests
} 